if not CLIENT then return end
require( "WinAmp_Interface" )
toggleBool = false
crosshairBool = false
maxHP = nil
maxArmor = nil
mdlPanel = nil
blahBool = false
czCurrentSong = " "
iTrackPercent = 0
szCurTimePlayed = " "
szCurTotalTime = " "
drool_enable = CreateClientConVar( "drool_enable", "1", true, false ) 
drool_enablebottombar = CreateClientConVar( "drool_enablebottombar", "1", true, false ) 
drool_enabletopbar = CreateClientConVar( "drool_enabletopbar", "1", true, false ) 
drool_enablemodel = CreateClientConVar( "drool_enablemodel", "1", true, false ) 
drool_winamp = CreateClientConVar( "drool_winamp", "1", true, false ) 
drool_crosshair = CreateClientConVar( "drool_crosshair", "1", true, false ) 
drool_hpbars = CreateClientConVar( "drool_hpbars", "1", true, false ) 
drool_integrate_utime = CreateClientConVar( "drool_integrate_utime", "1", true, false )
drool_24hclock = CreateClientConVar( "drool_24hclock", "1", true, false )
drool_panelcolor_r = CreateClientConVar( "drool_panelcolor_r", "256.0", true, false ) 
drool_panelcolor_g = CreateClientConVar( "drool_panelcolor_g", "256.0", true, false ) 
drool_panelcolor_b = CreateClientConVar( "drool_panelcolor_b", "256.0", true, false ) 
drool_panelalpha = CreateClientConVar( "drool_panelalpha", "256.0", true, false ) 

drool_shadecolor_r = CreateClientConVar( "drool_shadecolor_r", "256.0", true, false ) 
drool_shadecolor_g = CreateClientConVar( "drool_shadecolor_g", "256.0", true, false ) 
drool_shadecolor_b = CreateClientConVar( "drool_shadecolor_b", "256.0", true, false ) 
drool_shadealpha = CreateClientConVar( "drool_shadealpha", "256.0", true, false )

drool_textcolor_r = CreateClientConVar( "drool_textcolor_r", "256.0", true, false ) 
drool_textcolor_g = CreateClientConVar( "drool_textcolor_g", "256.0", true, false ) 
drool_textcolor_b = CreateClientConVar( "drool_textcolor_b", "256.0", true, false ) 
drool_textalpha = CreateClientConVar( "drool_textalpha", "256.0", true, false ) 

Drool_HideHud_Health = CreateClientConVar( "drool_hidehud_health", "0", true , false )
Drool_HideHud_Ammo = CreateClientConVar( "drool_hidehud_ammo", "0", true , false )
Drool_HideHud_Armor = CreateClientConVar( "drool_hidehud_armor", "0", true , false )

--Taken straight from UTime -.-
function timeToStr( time )
	local tmp = time
	local s = tmp % 60
	tmp = math.floor( tmp / 60 )
	local m = tmp % 60
	tmp = math.floor( tmp / 60 )
	local h = tmp % 24
	tmp = math.floor( tmp / 24 )
	local d = tmp % 7
	--tmp = math.floor( tmp / 7 )
	local w = tmp / 7
	
	return string.format( "%02iw %id %02ih %02im %02is", w, d, h, m, s )
end

utimecheck = file.Exists("../lua/autorun/cl_utime.lua")
function createModel()
	if not blahBool then
	mdlPanel = vgui.Create( "DModelPanel" )
		mdlPanel:SetSize( 180, 180 )
		mdlPanel:SetPos( 0, 0 )
		mdlPanel:SetModel( LocalPlayer():GetModel() )
		mdlPanel:SetAnimSpeed( 0.5 )
		mdlPanel:SetAnimated( false )
		mdlPanel:SetAmbientLight( Color( 50, 50, 50 ) )
		mdlPanel:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
		mdlPanel:SetDirectionalLight( BOX_FRONT, Color( 255, 255, 255 ) )
		mdlPanel:SetCamPos( Vector( 50, 50, 50 ) )
		mdlPanel:SetLookAt( Vector( 0, 0, 40 ) )
		mdlPanel:SetFOV( 70 )
		mdlPanel:SetVisible( false )
	end
	blahBool = true
end
hook.Add( "HUDPaint", "createModel", createModel )
	
function HUDPaint()

	function moveModel( x, y )
		if mdlPanel != nil then
			mdlPanel:SetPos( x, y )	
			mdlPanel:SetModel( LocalPlayer():GetModel() )
		end
	end
	function hideModel()
		if mdlPanel != nil then
			mdlPanel:SetModel( LocalPlayer():GetModel() )
		end
	end
	hook.Add( "Think", "hideModel", hideModel )
	local drool_panelcolor = Color( drool_panelcolor_r:GetInt(), drool_panelcolor_g:GetInt(), drool_panelcolor_b:GetInt(), drool_panelalpha:GetInt() )
	local drool_textcolor = Color( drool_textcolor_r:GetInt(), drool_textcolor_g:GetInt(), drool_textcolor_b:GetInt(), drool_textalpha:GetInt() )
	local drool_shadecolor = Color( drool_shadecolor_r:GetInt(), drool_shadecolor_g:GetInt(), drool_shadecolor_b:GetInt(), drool_shadealpha:GetInt() )
	surface.CreateFont( "Arial", 40, 500, true, false, "BigFont" )
	surface.CreateFont( "Arial", 25, 500, true, false, "MedFont" )
	local trace = {}
		trace.start = LocalPlayer():GetShootPos()
		trace.endpos = LocalPlayer():GetShootPos() + (LocalPlayer():GetAimVector() * 5000)
		trace.filter = LocalPlayer()
	local tr = util.TraceLine( trace )
	local client = LocalPlayer()
	--Winamp stuff
	if winamp != nil then
		szCurrentSong = winamp.GetCurrentTrack()
		iTrackPercent = winamp.TrackPercent()
		szCurTimePlayed = winamp.GetSongPosS()
		szCurTotalTime = winamp.GetSongLenS()
		iPlayMode = winamp.CurrentPlayMode()
		szPlayBuf = "[Stopped]"
		if(iPlayMode == 0) then
			szPlayBuf = "[Stopped]"
		elseif(iPlayMode == 1) then
			szPlayBuf = "[Playing]"
		elseif(iPlayMode == 3) then
			szPlayBuf = "[Paused]"
		end
	end
	if maxHP == nil then
		maxHP = 1
	end
	if maxArmor == nil then
		maxArmor = 1
	end
	if client:Health() > maxHP then maxHP = client:Health() end
	if client:Armor() != nil and client:Armor() > maxArmor then maxArmor = client:Armor() end
	if client:Health() <= 0 then
		maxHP = nil
	end
	--HP, Armor Panel Shade
	draw.RoundedBox( 4, -25 , ScrH()-95 , 330 , 160, drool_shadecolor )
	
	--HP Panel
	draw.RoundedBox( 4, -20 , ScrH()-90 , 320 , 150, drool_panelcolor )
	
	if drool_hpbars:GetBool() then
	
		draw.RoundedBox( 8, 10 , ScrH()-55 , 280 , 20, Color(175, 0, 0, 255) )
		if client:Armor() != nil then
			draw.RoundedBox( 8, 10 , ScrH()-30 , 280 , 20, Color(150, 175, 0, 255) )
		end
		if client:Health() != nil and client:Health() != 0 and maxHP != nil then
			draw.RoundedBox( 8, 10 , ScrH()-55 , ( 280 * ( client:Health()/maxHP ) ) , 20, Color(255, 0, 0, 255) )
		end
		if client:Armor() != nil and client:Armor() != 0 and maxArmor != nil then
			draw.RoundedBox( 8, 10 , ScrH()-30 , ( 280 * ( client:Armor()/maxArmor ) ) , 20, Color(230, 255, 0, 255) )
		end
		draw.SimpleText(client:Health() .. "%", "default", 150, ScrH()-45, Color(0, 255, 0, 255), 1, 1)
		if client:Armor() != nil then
			draw.SimpleText(client:Armor() .. "%", "default", 150, ScrH()-20, Color(255, 0, 0, 255), 1, 1)
		end
	else
		draw.SimpleText(client:Health() .. "%", "BigFont", 140, ScrH()-30, Color(0, 255, 0, 255), 2, 1)
		if client:Armor() != nil then
			draw.SimpleText(client:Armor() .. "%", "BigFont", 280, ScrH()-30, Color(255, 255, 0, 255), 2, 1)
		end
	end
	if client:GetActiveWeapon() != NULL and not (client:GetActiveWeapon():Clip1() <= 0 and client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) <= 0 and client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) <= 0) then
		if drool_enablemodel:GetBool() then
			draw.RoundedBox( 4, ScrW()-155 , ScrH()-270 , 155 , 210, drool_shadecolor ) --ModelPanel
			draw.RoundedBox( 4, ScrW()-150 , ScrH()-265 , 145 , 200, drool_panelcolor )
		end
		moveModel( ScrW()-170, ScrH()-265 )
		if drool_enablebottombar:GetBool() then
			draw.RoundedBox( 4, 300 , ScrH()-40 , ScrW()-555 , 70, drool_shadecolor )
			draw.RoundedBox( 4, 305 , ScrH()-35 , ScrW()-560 , 60, drool_panelcolor ) --Bottom bar
		end
		draw.RoundedBox( 4, ScrW()-255 , ScrH()-65 , 260 , 70, drool_shadecolor )
		draw.RoundedBox( 4, ScrW()-250 , ScrH()-60 , 260 , 70, drool_panelcolor ) --Ammobox
		if client:GetActiveWeapon():Clip1() > 0 then
			draw.SimpleText(client:GetActiveWeapon():Clip1() , "BigFont", ScrW()-230, ScrH()-30, Color(0, 255, 0, 255), 0, 1)
		end
		if client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) > 0 then
			draw.SimpleText(client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) , "BigFont", ScrW()-150, ScrH()-30, Color(255, 255, 0, 255), 0, 1)
		end
		if client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType()) > 0 then
			draw.SimpleText(client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType()) , "BigFont", ScrW()-50, ScrH()-30, Color(255, 0, 0, 255), 0, 1)
		end
	else
		if drool_enablemodel:GetBool() and drool_enablebottombar:GetBool() then
			draw.RoundedBox( 4, ScrW()-155 , ScrH()-245 , 155 , 210, drool_shadecolor ) --ModelPanel
			draw.RoundedBox( 4, ScrW()-150 , ScrH()-240 , 145 , 200, drool_panelcolor )
			moveModel( ScrW()-170, ScrH()-240 )
		elseif drool_enablemodel:GetBool() and not drool_enablebottombar:GetBool() then
			draw.RoundedBox( 4, ScrW()-150 , ScrH()-205 , 155 , 210, drool_shadecolor ) --ModelPanel
			draw.RoundedBox( 4, ScrW()-145 , ScrH()-200 , 145 , 200, drool_panelcolor )
			moveModel( ScrW()-165, ScrH()-200 )
		end
		if drool_enablebottombar:GetBool() then
			draw.RoundedBox( 4, 300 , ScrH()-40 , ScrW()-295 , 70, drool_shadecolor )
			draw.RoundedBox( 4, 305 , ScrH()-35 , ScrW()-305 , 60, drool_panelcolor )
		end
	end
	if utimecheck and client:GetUTimeTotalTime() != nil and drool_integrate_utime:GetBool() then
		if tr.Entity:IsPlayer() then
				draw.RoundedBox( 4, 0, 0, 265, 120, drool_shadecolor )
				draw.SimpleText( "Name: " .. tr.Entity:Nick(), "ScoreboardText", 5, 70, team.GetColor( tr.Entity:Team() ), 0, 1)
				draw.SimpleText( "Total time: " .. timeToStr( tr.Entity:GetUTime() + CurTime() - tr.Entity:GetUTimeStart() ), "ScoreboardText", 5, 90, drool_textcolor, 0, 1)
				draw.SimpleText( "Session time: " .. timeToStr(tr.Entity:GetUTimeSessionTime()), "ScoreboardText", 5, 110, drool_textcolor, 0, 1)
		end
		if drool_enabletopbar:GetBool() then
			draw.RoundedBox( 4, 260 , -35 , ScrW()-255 , 65, drool_shadecolor )
			draw.RoundedBox( 4, 265 , -30 , ScrW()-265 , 55, drool_panelcolor )
		end
		draw.RoundedBox( 4, -105, -25, 370, 90, drool_shadecolor )
		draw.RoundedBox( 4, -100, -20, 360, 80, drool_panelcolor )
		draw.SimpleText( "Name: " .. client:Nick(), "ScoreboardText", 5, 10, team.GetColor( client:Team() ), 0, 1)
		draw.SimpleText( "Total time: " .. timeToStr(client:GetUTimeTotalTime()), "ScoreboardText", 5, 30, drool_textcolor, 0, 1)
		draw.SimpleText( "Session time: " .. timeToStr(client:GetUTimeSessionTime()), "ScoreboardText", 5, 50, drool_textcolor, 0, 1)
	else
		if drool_enabletopbar:GetBool() then
		--Tob bar shade
			draw.RoundedBox( 4, -40 , -35 , ScrW()+40 , 65, drool_shadecolor )
			--Tob bar
			draw.RoundedBox( 4, -35 , -30 , ScrW()+35 , 55, drool_panelcolor )
		end
	end
	if winamp != nil then
		local bFoundWinAmp = winamp.SetupWinAmp()
		if bFoundWinAmp and drool_winamp:GetBool() then
			local iWidth = iTrackPercent * 3.2
			draw.RoundedBox( 8, ScrW()/2-175, 25, 350, 60, drool_shadecolor )
			draw.RoundedBox( 8, ScrW()/2-170, 30, 340, 50, drool_panelcolor )
			draw.RoundedBox( 8, ScrW()/2-160, 40, 320, 15, Color(130, 0, 0, 255) )
			if iTrackPercent != 0 then
				draw.RoundedBox( 8, ScrW()/2-160, 40, iWidth, 15, Color(255, 0, 0, 255) )
			end
			draw.SimpleText( szCurrentSong, "default", ScrW()/2-155, 47, drool_textcolor, 0, 1)
			draw.SimpleText( szCurTimePlayed .. " / " .. szCurTotalTime, "ScoreboardText", ScrW()/2+155, 65, drool_textcolor, 2, 1)
			draw.SimpleText( szPlayBuf , "ScoreboardText", ScrW()/2, 65, drool_textcolor, 1, 1)
		end
	end
	if drool_enabletopbar:GetBool() then
		if drool_24hclock:GetBool() then
			draw.SimpleText(os.date("%H:%M:%S"), "MedFont", ScrW()/2, 12, drool_textcolor, 1, 1)
		else
			draw.SimpleText(os.date("%I:%M:%S %p"), "MedFont", ScrW()/2, 12, drool_textcolor, 1, 1)
		end
	end
	local pos = client:GetPos()
	draw.SimpleText( math.floor(pos.x) .. "  " .. math.floor(pos.y) .. "  " .. math.floor(pos.z), "ScoreboardText", 150, ScrH()-75, drool_textcolor, 1, 1 )
	if drool_enabletopbar:GetBool() then
		draw.SimpleText( math.floor(1/RealFrameTime()) .. " fps" , "ScoreboardText", ScrW()/2+350, 12, drool_textcolor, 2, 1 )
		draw.SimpleText(client:Ping() .. " ms", "ScoreboardText", ScrW()/2+300, 12, drool_textcolor, 2, 1)
	end
	if drool_enablebottombar:GetBool() then
		if not tr.HitWorld and tr.Entity and tr.Entity:IsValid() and not tr.Entity:IsPlayer() then
			draw.SimpleText("Model: " .. tr.Entity:GetModel(), "ScoreboardText", ScrW()/2, ScrH()-23, drool_textcolor, 1, 1)
			draw.SimpleText("Pos: " .. tostring(tr.Entity:GetPos()), "ScoreboardText", ScrW()/2, ScrH()-9, drool_textcolor, 1, 1)
		end
		if tr.Entity:IsPlayer() then
			draw.SimpleText("Name: " .. tr.Entity:Nick(), "ScoreboardText", ScrW()/2, ScrH()-23, drool_textcolor, 1, 1)
			draw.SimpleText("Pos: " .. tostring(tr.Entity:GetPos()), "ScoreboardText", ScrW()/2, ScrH()-9, drool_textcolor, 1, 1)
		end
	end
	if client:GetActiveWeapon() != NULL then
		if client:GetActiveWeapon():GetPrintName() == "#GMOD_Camera" or not drool_enable:GetBool() or not drool_enablemodel:GetBool() then
			randomBool = false
		else 
			randomBool = true 
		end
		if mdlPanel != nil then
			mdlPanel:SetVisible( randomBool )
		end
	end
end

--Control Panel stuff
function toggle()
	local client = LocalPlayer()
	if drool_enable:GetBool() and not toggleBool then
		RunConsoleCommand( "drool_enable", "1" )
		hook.Add( "HUDPaint", "TestPaint", HUDPaint )
		toggleBool = true
	elseif (not drool_enable:GetBool() and toggleBool) then
		RunConsoleCommand( "drool_enable", "0" )
		hook.Remove( "HUDPaint", "TestPaint")
		toggleBool = false
	end
end
hook.Add( "Think", "toggleThink", toggle )

function resetCvars()
	RunConsoleCommand( "drool_panelcolor_r", "60" )
	RunConsoleCommand( "drool_panelcolor_g", "60" )
	RunConsoleCommand( "drool_panelcolor_b", "60" )
	RunConsoleCommand( "drool_panelalpha", "200" )
	
	RunConsoleCommand( "drool_shadecolor_r", "25" )
	RunConsoleCommand( "drool_shadecolor_g", "25" )
	RunConsoleCommand( "drool_shadecolor_b", "25" )
	RunConsoleCommand( "drool_shadealpha", "255" )
	
	RunConsoleCommand( "drool_textcolor_r", "255" )
	RunConsoleCommand( "drool_textcolor_g", "0" )
	RunConsoleCommand( "drool_textcolor_b", "0" )
	RunConsoleCommand( "drool_textalpha", "255" )

	buildCP( GetControlPanel( "DroolHUD Settings" ) )
end
concommand.Add( "drool_reset", resetCvars )

function buildCP( cpanel )
	cpanel:ClearControls()
	cpanel:AddControl( "Header", { Text = "DroolHUD by raBBish/Polly" } )
	cpanel:AddControl( "CheckBox", { Label = "Use Health Bars", Command = "drool_hpbars" }  )
	cpanel:AddControl( "CheckBox", { Label = "Show Top Bar", Command = "drool_enabletopbar" }  )
	cpanel:AddControl( "CheckBox", { Label = "Show Bottom Bar", Command = "drool_enablebottombar" }  )
	cpanel:AddControl( "CheckBox", { Label = "Show Model", Command = "drool_enablemodel" }  )
	cpanel:AddControl( "CheckBox", { Label = "Integrate UTime", Command = "drool_integrate_utime" }  )
	cpanel:AddControl( "CheckBox", { Label = "Show WinAmp HUD", Command = "drool_winamp" }  )
	cpanel:AddControl( "CheckBox", { Label = "Toggle Crosshair", Command = "drool_crosshair" }  )
	cpanel:AddControl( "CheckBox", { Label = "Use 24h Clock", Command = "drool_24hclock" }  )
	cpanel:AddControl( "CheckBox", { Label = "Hide Default HUD Health", Command = "drool_hidehud_health" }  )
	cpanel:AddControl( "CheckBox", { Label = "Hide Default HUD Armor", Command = "drool_hidehud_armor" }  )
	cpanel:AddControl( "CheckBox", { Label = "Hide Default HUD Ammo", Command = "drool_hidehud_ammo" }  )
	cpanel:AddControl( "Color", { Label = "Panel Color", Description = "Panel Color" , Red = "drool_panelcolor_r", Green = "drool_panelcolor_g", Blue = "drool_panelcolor_b", Alpha = "drool_panelalpha", ShowAlpha = "1", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" }  )
	cpanel:AddControl( "Color", { Label = "Panel Shade Color", Red = "drool_shadecolor_r", Green = "drool_shadecolor_g", Blue = "drool_shadecolor_b", Alpha = "drool_shadealpha", ShowAlpha = "1", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" }  )
	cpanel:AddControl( "Color", { Label = "Text Color", Red = "drool_textcolor_r", Green = "drool_textcolor_g", Blue = "drool_textcolor_b", Alpha = "drool_textalpha", ShowAlpha = "1", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" }  )
	cpanel:AddControl( "Button", { Text = "Reset", Label = "Reset colors", Command = "drool_reset" } )
end

function spawnMenuOpen()
	buildCP( GetControlPanel( "DroolHUD Settings" ) )
end
hook.Add( "SpawnMenuOpen", "DroolSpawnMenuOpen", spawnMenuOpen )

function popToolMenu()
	spawnmenu.AddToolMenuOption( "Options", "DroolHUD", "DroolHUD Settings", "DroolHUD Settings", "", "", buildCP, { SwitchConVar = "drool_enable" } )
	if drool_panelcolor_r:GetInt() == 256 then resetCvars() end	
end
hook.Add( "PopulateToolMenu", "DroolPopulateTools", popToolMenu )

function Drool_HideHUD ( OptionName )
	if OptionName == "CHudHealth" and Drool_HideHud_Health:GetBool() then
		return false
	elseif OptionName == "CHudBattery" and Drool_HideHud_Armor:GetBool() then
		return false
	elseif (OptionName == "CHudAmmo" or OptionName == "CHudSecondaryAmmo") and Drool_HideHud_Ammo:GetBool() then
		return false
	end
end
hook.Add( "HUDShouldDraw", "Drool_HideHUD", Drool_HideHUD ) 

function drawXHair()
	surface.SetMaterial( Material( "xhair/xhair" ) )
	surface.SetDrawColor( 255, 0, 0, 255 )
	surface.DrawTexturedRect( ScrW() / 2 - 64 / 2, ScrH() / 2 - 64 / 2, 64, 64 )
end

function XHair_HUDShouldDraw(sID)
	if sID == "CHudCrosshair" then return false end
end

function xhairtoggle()
	if drool_crosshair:GetBool() and not crosshairBool then
		hook.Add("HUDShouldDraw", "XHair_HUDShouldDraw", XHair_HUDShouldDraw)
		hook.Add("HUDPaint", "drawXHair", drawXHair )
		crosshairBool = true
	elseif not drool_crosshair:GetBool() and crosshairBool then
		hook.Remove("HUDShouldDraw", "XHair_HUDShouldDraw")
		hook.Remove( "HUDPaint", "drawXHair", drawXHair )
		crosshairBool = false
	end
end
hook.Add( "Think", "xhairtoggle", xhairtoggle )
   